home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Graphics / STIMP_noise / source / include / STIMP / misc.c next >
Encoding:
C/C++ Source or Header  |  1998-02-14  |  5.6 KB  |  271 lines

  1.  
  2. // common declarations and routines
  3. // Version 2.16, 03.Feb.98
  4. // (c) 1997/98 by Stefan Diener
  5.  
  6. #ifndef STIMP_MISC_INC
  7. #define STIMP_MISC_INC
  8.  
  9. // The next define makes it impossible to read PBM- and PPM-files for PGM-only operators !!!
  10. // - PBM -> b/w PGM
  11. // - PGM (as usual)
  12. // - PPM -> red color channel as PGM
  13. // If you want to use this feature comment the following line !
  14. //#define NO_PSEUDO_PGM_MODE
  15.  
  16. // The next define makes it impossible to read PBM- and PGM-files for PPM-only operators !!!
  17. // - PBM -> b/w PPM
  18. // - PGM -> gray PPM
  19. // - PPM (as usual)
  20. // If you want to use this feature comment the following line !
  21. //#define NO_PSEUDO_PPM_MODE
  22.  
  23. // define TRUE, FALSE and BOOL if not already done in <sys/types.h>
  24. #ifndef TRUE
  25. #define TRUE        1
  26. #endif
  27.  
  28. #ifndef FALSE
  29. #define FALSE        0
  30. #endif
  31.  
  32. #ifndef BOOL
  33. #define BOOL int
  34. #endif
  35.  
  36. // define some types
  37. #define TYPE_PBM 1
  38. #define TYPE_PGM 2
  39. #define TYPE_PPM 4
  40.  
  41. // Define a function for correcting errors due fgets.
  42. // Please insert correct(string) always after using fgets,
  43. // because of (UNIX-)compatibility.
  44. #define correct(s) if ((s[0]) && (s[strlen(s)-1]==10)) s[strlen(s)-1]=0
  45.  
  46. // version string for AmigaOS
  47. const char _AMIGA_Version[]="$VER: "OP_NAME" "VERSION" ("DATE") "AUTHOR;
  48.  
  49. static int beVerbose=TRUE;   // print messages ? default is YES !
  50.  
  51. // declare some functions
  52. void Hilfe(void);
  53. char *GetFilename(int nummer, int argc,char **argv);
  54.  
  55. // define some functions
  56. void PrintMessage(char *fmt, ...)
  57. // standard output routine
  58. {
  59.   va_list args;
  60.  
  61.   if (beVerbose)
  62.   {
  63.     va_start(args,fmt);
  64.     fprintf(stderr,OP_NAME": ");
  65.     vfprintf(stderr,fmt,args);
  66.     fprintf(stderr,"\n");
  67.     va_end(args);
  68.   }
  69. }
  70.  
  71. void PrintOpening(int argc, char **argv)
  72. // standard welcome message
  73. {
  74.   int i;
  75.   unsigned char *temp;
  76.  
  77.   if (argc==1) beVerbose=TRUE;
  78.   else
  79.     for (i=1; i<argc; i++)
  80.     {
  81.       temp=GetFilename(i,argc,argv);
  82.       if (temp==NULL) i=argc;
  83.       else if (strcmp("-",temp)==0) { i=argc; beVerbose=FALSE; }
  84.     }
  85.  
  86.   if (beVerbose) fprintf(stderr,OP_NAME": "VERSION" ("DATE")  (c) "AUTHOR"\n");
  87.  
  88.   if (argc==1)
  89.   {
  90.     Hilfe();
  91.     exit(0);
  92.   }
  93. }
  94.  
  95. void PrintClosing(void)
  96. // standard end message
  97. {
  98.   if (beVerbose) fprintf(stderr,OP_NAME": Done !\n");
  99. }
  100.  
  101. void ReadComment(FILE *datei)
  102. // skip one or more lines of comments in a PBM/PGM/PPM-file
  103. {
  104.   int wert;
  105.  
  106.   do
  107.   {
  108.     wert=fgetc(datei);
  109.     if (wert!=EOF)   // EOF while reading header ?
  110.     {
  111.       if (wert==0x23)   // '#' = start of a comment line
  112.       {
  113.         do
  114.         {
  115.           wert=fgetc(datei);
  116.           if (wert==EOF) wert=0x0a;   // end of file = abort
  117.         }
  118.         while (wert!=0x0a);   // end of line
  119.         wert=0x23;
  120.       }
  121.       else fseek(datei,-1,SEEK_CUR);   // seek one position left
  122.     }
  123.   }
  124.   while (wert==0x23);
  125. }
  126.  
  127. int GetType(char *name)
  128. // get type of a file
  129. // result: 0 = file not found, TYPE_PBM, TYPE_PGM, TYPE_PPM, other = error
  130. {
  131.   FILE *datei;
  132.   char tempo[10];
  133.   int type=128;
  134.  
  135.   if (strcmp("-",name)==0) datei=stdin;   // use stdin
  136.   else
  137.   {
  138.     // really open a file
  139.     datei=fopen(name,"rb");
  140.     if (datei==0)
  141.     {
  142.       PrintMessage("File %s not found !", name);
  143.       return 0;
  144.     }
  145.   }
  146.  
  147.   // set buffer size
  148.   setvbuf(datei,0,_IOFBF,500);
  149.  
  150.   // read 1st line of the header
  151.   fgets(tempo, 9, datei);
  152.   correct(tempo);
  153.  
  154.   // check the type
  155.   if (strcmp(tempo,"P4")==0) type=TYPE_PBM;
  156.   if (strcmp(tempo,"P5")==0) type=TYPE_PGM;
  157.   if (strcmp(tempo,"P6")==0) type=TYPE_PPM;
  158.  
  159.   // close file and exit
  160.   if (strcmp("-",name)!=0) fclose(datei);
  161.   return type;
  162. }
  163.  
  164. int FilenameCount(int argc,char **argv)
  165. // calculates the number of files in the commandline
  166. {
  167.   int i, counter=0, std=0;
  168.  
  169.   // counting
  170.   for (i=1; i<argc; i++)
  171.   {
  172.     if ((argv[i][0]!='-') && (argv[i][0]!='+')) counter++;
  173.     else if ((argv[i][0]=='-') && (argv[i][1]==0)) std++;
  174.   }
  175.  
  176.   if (std>2) return (0);
  177.   else return (counter+std);
  178. }
  179.  
  180. char *GetFilename(int nummer, int argc,char **argv)
  181. // looks for the ...th filename in the commandline
  182. {
  183.   int i, counter=0;
  184.  
  185.   // at least number one
  186.   if (nummer<1) return NULL;
  187.  
  188.   // counting
  189.   for (i=1; i<argc; i++)
  190.   {
  191.     if (((argv[i][0]!='-') && (argv[i][0]!='+')) || ((argv[i][0]=='-') && (argv[i][1]==0))) counter++;
  192.     if (counter==nummer)
  193.     {
  194.       // successful
  195.       counter=i;
  196.       nummer=i;
  197.       i=argc;
  198.     }
  199.   }
  200.  
  201.   // filename found ?
  202.   return ((nummer==counter) ? argv[counter] : NULL);
  203. }
  204.  
  205. void Hilfe(void)
  206. // print help file on screen
  207. {
  208.   char name[200], *variable;
  209.   FILE *datei;
  210.  
  211.   // filename and suffix, 1st try
  212.   strcpy(name,OP_NAME);
  213.   strcat(name,".syntax");
  214.  
  215.   // open file, 1st try
  216.   datei=fopen(name,"rb");
  217.   if (datei==0)
  218.   {
  219.     // help file not in local directory
  220.     // cat the name of the helppath via AmigaOS-Assign, 2nd try
  221.     strcpy(name,"STIMP:help/");
  222.     strcat(name,OP_NAME);
  223.     strcat(name,".syntax");
  224.  
  225.     // open file, 2nd try
  226.     datei=fopen(name,"rb");
  227.     if (datei==0)
  228.     {
  229.       // cat the name of the helppath via env, 3rd try
  230.       variable=getenv("STIMP");
  231.       strcpy(name,variable);
  232.       strcat(name,"/help/");
  233.       strcat(name,OP_NAME);
  234.       strcat(name,".syntax");
  235.  
  236.       // open file, 3rd try
  237.       datei=fopen(name,"rb");
  238.       if (datei==0)
  239.       {
  240.         // not found, giving up
  241.         PrintMessage("No help available (helpfile not found) !");
  242.         PrintClosing();
  243.         return;
  244.       }
  245.     }
  246.   }
  247.  
  248.   // set buffer size
  249.   setvbuf(datei,0,_IOFBF,1000);
  250.  
  251.   // preamble
  252.   PrintMessage("Help:");
  253.   PrintMessage("");
  254.  
  255.   // read file and print it on the screen
  256.   while (fgets(name, 100, datei)!=NULL)
  257.   {
  258.     correct(name);
  259.     PrintMessage("%s", name);
  260.   }
  261.  
  262.   // close file
  263.   fclose(datei);
  264.  
  265.   // one more line and goodbye
  266.   PrintMessage("");
  267.   PrintClosing();
  268. }
  269.  
  270. #endif
  271.